package com.itheima.service;

import com.itheima.dao.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/1
 */
public class StudentService implements StudentMapper{
    @Override
    public List<Student> selectAll() {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> list = mapper.selectAll();
        sqlSession.close();
        return list;
    }

    @Override
    public void add(Student student) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.add(student);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Student selectById(int id) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(id);
        sqlSession.close();
        return student;
    }

    @Override
    public void update(Student student) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.update(student);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void delete(int id) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        mapper.delete(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Student selectBySname(String sname) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectBySname(sname);
        sqlSession.close();
        return student;
    }
}
